/*
 * @author: wizardc
 */

using System;
using System.Collections.Generic;
using UnityEngine;

namespace Dou.ScreenController
{
    /// <summary>
    /// 屏幕适配数据，颜色
    /// </summary>
    [Serializable]
    public class UIAdapterColor
    {
        public List<UIAdapterColorData> dataList = new List<UIAdapterColorData>();
        
        public bool GetValue(int modeIndex, int fineTuningIndex, out Color result)
        {
            result = Color.white;
            
            if (modeIndex >= dataList.Count)
            {
                return false;
            }
            
            var data = dataList[modeIndex];
            if (!data.isInit)
            {
                return false;
            }

            result = data.baseData;

            if (fineTuningIndex >= 0 && fineTuningIndex < data.fineTuningList.Count)
            {
                var fineTuning = data.fineTuningList[fineTuningIndex];
                if (fineTuning.data.HasValue)
                {
                    result = fineTuning.data.Value;
                }
            }

            return true;
        }
        
#if UNITY_EDITOR

        public void Init(UIScreenMode screenMode, UIFineTuningMode fineTuningMode, Color value)
        {
            InitData();
            TryRecordBase((int) screenMode, value);
        }

        public void Record(UIScreenMode screenMode, UIFineTuningMode fineTuningMode, Color value)
        {
            var uiScreenMode = (int) screenMode;
            var uiFineTuningMode = (int) fineTuningMode;
            
            TryRecordBase(uiScreenMode, value);
            if (fineTuningMode == UIFineTuningMode.None)
            {
                dataList[uiScreenMode].baseData = value;
            }
            else
            {
                dataList[uiScreenMode].fineTuningList[uiFineTuningMode].data = value;
            }
        }

        private void InitData()
        {
            const int fineTuningMax = (int) UIFineTuningMode.Max;
            if (dataList.Count == 0)
            {
                for (int i = 0; i < (int) UIScreenMode.Max; i++)
                {
                    var data = new UIAdapterColorData();
                    for (int j = 0; j < fineTuningMax; j++)
                    {
                        data.fineTuningList.Add(new UIAdapterColorFineTuningData());
                    }
                    dataList.Add(data);
                }
            }
            if (dataList[0].fineTuningList.Count != fineTuningMax)
            {
                for (int i = 0; i < (int) UIScreenMode.Max; i++)
                {
                    var list = dataList[i].fineTuningList;
                    if (list.Count > fineTuningMax)
                    {
                        list.RemoveRange(0, fineTuningMax);
                    }
                    else
                    {
                        for (int j = list.Count; j < fineTuningMax; j++)
                        {
                            list.Add(new UIAdapterColorFineTuningData());
                        }
                    }
                }
            }
        }

        private void TryRecordBase(int uiScreenMode, Color value)
        {
            var data = dataList[uiScreenMode];
            if (!data.isInit)
            {
                data.isInit = true;
                data.baseData = value;
            }
        }
        
#endif
        
    }
    
    [Serializable]
    public class UIAdapterColorData
    {
        public bool isInit;
        public Color baseData;
        public List<UIAdapterColorFineTuningData> fineTuningList = new List<UIAdapterColorFineTuningData>();
    }
    
    [Serializable]
    public class UIAdapterColorFineTuningData
    {
        public Color? data;
    }
}
